﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Models;
namespace BookStore
{
    [HandleError]
    public class Pagination
    {
        string url;
        int total_page, cur_page;
        public Pagination(string _url,int _total_book,int _limit, int _cur_page)
        {
            int _total_page = _total_book / _limit;
            if (_total_book % _limit != 0) _total_page++;

            url = _url;
            total_page=_total_page;
            cur_page=_cur_page;
        }

        public string render()
        {
            string html = "";
            if (total_page > 1)
            {
                if (cur_page < 4)
                    html += "<span class='disabled'><<</span>";
                else
                {
                    html += " <a href='" + url + ((cur_page - 10 < 1) ? 1 : cur_page - 10) + "'><<</a>";
                    html += "…";
                }

                if (cur_page > 3 && cur_page < total_page - 2)
                {
                    for (int i = cur_page - 2; i <= cur_page + 2; i++)
                    {
                        if (cur_page == i)
                            html += " <span class='current'>" + i + "</span>";
                        else
                            html += "<a href='" + url + i + "'>" + i + "</a>";
                    }
                }
                else if (cur_page <= 3)
                {
                    for (int i = 1; i <= (total_page < 5 ? total_page : 5); i++)
                    {
                        if (cur_page == i)
                            html += " <span class='current'>" + i + "</span>";
                        else
                            html += " <a href='" + url + i + "'>" + i + "</a>";
                    }
                }
                else
                {
                    for (int i = total_page - (total_page < 5 ? 3 : 4); i <= total_page; i++)
                    {
                        if (cur_page == i)
                            html += " <span class='current'>" + i + "</span>";
                        else
                            html += " <a href='" + url + i + "'>" + i + "</a>";
                    }
                }

                if (cur_page > total_page - 3)
                    html += "<span class='disabled'>>></span>";
                else
                {
                    html += " …";
                    html += "<a href='" + url + ((cur_page + 10 > total_page) ? total_page : cur_page + 10) + " '>>></a>";
                }
            }
            return html;
        }

        public string renderJson()
        {
            string html = "";
            if (total_page > 1)
            {
                if (cur_page < 4)
                    html += "<span class='disabled'><<</span>";
                else
                {
                    html += " <a href='javascript:;' onclick=jsonPage('" + url + ((cur_page - 10 < 1) ? 1 : cur_page - 10) + "')><<</a>";
                    html += "…";
                }

                if (cur_page > 3 && cur_page < total_page - 2)
                {
                    for (int i = cur_page - 2; i <= cur_page + 2; i++)
                    {
                        if (cur_page == i)
                            html += " <span class='current'>" + i + "</span>";
                        else
                            html += "<a href='javascript:;' onclick=jsonPage('" + url + i + "')>" + i + "</a>";
                    }
                }
                else if (cur_page <= 3)
                {
                    for (int i = 1; i <= (total_page < 5 ? total_page : 5); i++)
                    {
                        if (cur_page == i)
                            html += " <span class='current'>" + i + "</span>";
                        else
                            html += " <a href='javascript:;' onclick=jsonPage('" + url + i + "')>" + i + "</a>";
                    }
                }
                else
                {
                    for (int i = total_page - (total_page < 5 ? 3 : 4); i <= total_page; i++)
                    {
                        if (cur_page == i)
                            html += " <span class='current'>" + i + "</span>";
                        else
                            html += " <a href='javascript:;' onclick=jsonPage('" + url + i + "')>" + i + "</a>";
                    }
                }

                if (cur_page > total_page - 3)
                    html += "<span class='disabled'>>></span>";
                else
                {
                    html += " …";
                    html += "<a href='javascript:;' onclick=jsonPage('" + url + ((cur_page + 10 > total_page) ? total_page : cur_page + 10) + "')>>></a>";
                }
            }
            return html;
        }
    }
}
